package aliyun.cx.service.controller;

import aliyun.cx.service.entity.User;
import aliyun.cx.service.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务模块控制器
 */
@RestController
@RequestMapping("/api/service")
public class ServiceController {
    
    private final UserService userService;

    public ServiceController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/users")
    public Map<String, Object> getUserList(@RequestParam(defaultValue = "1") Integer current,
                                          @RequestParam(defaultValue = "10") Integer size) {
        Page<User> page = new Page<>(current, size);
        Page<User> userPage = userService.page(page, new QueryWrapper<User>().eq("enabled", 1));
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "success");
        result.put("data", userPage);
        return result;
    }
    
    /**
     * 根据ID获取用户
     */
    @GetMapping("/users/{id}")
    public Map<String, Object> getUserById(@PathVariable Long id) {
        User user = userService.getById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (user != null) {
            result.put("code", 200);
            result.put("message", "success");
            result.put("data", user);
        } else {
            result.put("code", 404);
            result.put("message", "用户不存在");
        }
        return result;
    }
    
    /**
     * 创建用户
     */
    @PostMapping("/users")
    public Map<String, Object> createUser(@RequestBody User user) {
        boolean success = userService.save(user);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "创建成功");
            result.put("data", user);
        } else {
            result.put("code", 500);
            result.put("message", "创建失败");
        }
        return result;
    }
}
